import os
import logging

from src.python.tools.singleton import singleton


@singleton
class logger(object):
    """
    处理日志类
    """

    def __init__(self):
        # 配置日志输出位置
        self.path = os.getcwd() + "\日志.log"
        if os.path.isfile(self.path):
            os.remove(self.path)

        # 配置输出格式
        self.LOG_FORMAT = "%(asctime)s %(levelname)s %(message)s"
        # 配置输出时间的格式
        self.DATE_FORMAT = '%Y-%m-%d %H:%M:%S'
        self.setting()

    def setting(self):
        # 有了filename参数就不会直接输出显示到控制台，而是直接写入文件
        logging.basicConfig(level=logging.INFO,
                            format=self.LOG_FORMAT,
                            datefmt=self.DATE_FORMAT,
                            filename=self.path,
                            )

    def debug(self, msg):
        logging.debug(msg)

    def info(self, msg):
        logging.info(msg)

    def warning(self, msg):
        logging.warning(msg)

    def error(self, msg):
        logging.error(msg)

    def critical(self, msg):
        logging.critical(msg)
